﻿using Moqikaka.BLL;
using Moqikaka.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        #region 填充默认请求数据
        //string userId, int partnerId
        /// <summary>
        /// 检查玩家是否可以参加回归活动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PlayerReturnCheck_Click(object sender, EventArgs e)
        {
            RequestData data = new RequestData();
            data.ModuleName = "PlayerReturn";
            data.ActionName = "PlayerReturnCheck";

            data.Data = new List<object>();
            data.Data.Add("test1");
            data.Data.Add(12);

            this.txt_Request.Text = JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 设置玩家已经参加回归活动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SetPlayerReturnComplete_Click(object sender, EventArgs e)
        {
            RequestData data = new RequestData();
            data.ModuleName = "PlayerReturn";
            data.ActionName = "SetPlayerReturnComplete";

            data.Data = new List<object>();
            data.Data.Add("test1");
            data.Data.Add(12);

            this.txt_Request.Text = JsonConvert.SerializeObject(data);
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            RequestData data = new RequestData();
            data.ModuleName = "Login";
            data.ActionName = "UserLogin";

            data.Data = new List<object>();
            data.Data.Add("test1");
            data.Data.Add(12);

            this.txt_Request.Text = JsonConvert.SerializeObject(data);
        } 
        #endregion

        #region 开始请求
        private void btn_Go_Click(object sender, EventArgs e)
        {
            this.btn_Go.Enabled = false;

            //提取更新函数
            var showResponseFun = new Action<string>((txt) =>
            {
                this.Invoke(new Action(() =>
                {
                    this.txt_Response.Text = txt;
                }));
            });

            Task.Run(() =>
            {
                //开始请求
                string result = string.Empty;
                try
                {
                    result = HttpUtil.Post(txt_URL.Text.Trim(), this.txt_Request.Text.Trim());
                }
                catch (Exception ex)
                {
                    showResponseFun(ex.Message);
                    return;
                }
                finally
                {
                    this.Invoke(new Action(() =>
                    {
                        //启用按钮
                        this.btn_Go.Enabled = true;
                    }));

                }
                showResponseFun(result);

                //反序列化
                ResultResponse response = JsonConvert.DeserializeObject<ResultResponse>(result);

                //获取状态码的对应描述详情
                string desc = ReflectionUtil.GetEnumDesc<ResultCodeEnum>(response.ResultCode);

                showResponseFun(result + "\r\n------------------\r\n" + desc);


            });


        }
        
        #endregion

    }
}
